package com.niodata.dp.util;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.function.BinaryOperator;

public class StringUtil {

  public static boolean isEmpty(String str) {
    return str == null || str.trim().length() == 0;
  }

  public static boolean isNotEmpty(String str) {
    return str != null && str.trim().length() > 0;
  }

  public static String quoted(String str) {
    return "\"" + str + "\"";
  }

  public static String join(Object[] objects, String on) {
    String v = (String) Arrays.stream(objects).reduce(new BinaryOperator<Object>() {
      @Override
      public String apply(Object o, Object o2) {
        if (o == null) {
          o = "NULL";
        }
        if (o2 == null) {
          o2 = "NULL";
        }
        return o + on + o2;
      }
    }).get();
    return v;
  }

  public static String join(List objects, String on) {
    String v = (String) objects.stream().reduce(new BinaryOperator<Object>() {
      @Override
      public String apply(Object o, Object o2) {
        if (o == null) {
          o = "NULL";
        }
        if (o2 == null) {
          o2 = "NULL";
        }
        return o + on + o2;
      }
    }).get();
    return v;
  }

  public static String repeatChar(char ch, int repeat) {
    StringBuffer sbu = new StringBuffer();
    for (int i = 0; i < repeat; i++) {
      sbu.append(ch);
    }
    return sbu.toString();
  }

  public static String repeatString(String str, int repeat) {
    StringBuffer sbu = new StringBuffer();
    for (int i = 0; i < repeat; i++) {
      sbu.append(str);
    }
    return sbu.toString();
  }

  /**
   * 字符串固定列宽补全,左右对齐.
   *
   * @param columns 字符串列
   * @param columLens 列宽数组
   * @param padChar 补全字符
   * @return 补全字符串
   */
  public static String padStringColumns(String[] columns, int[] columLens, char padChar) {
    if (columLens.length != columns.length) {
      throw new RuntimeException("column count != len count");
    }
    StringBuffer stringBuffer = new StringBuffer();
    for (int i = 0; i < columLens.length; i++) {
      int padNum = columLens[i] - columns[i].length();
      if (i == columLens.length - 1) {
        if (padNum > 0) {
          stringBuffer.append(repeatChar(padChar, padNum));
        }
        stringBuffer.append(columns[i]);
      } else {
        stringBuffer.append(columns[i]);
        if (padNum > 0) {
          stringBuffer.append(repeatChar(padChar, padNum));
        }
      }
    }
    return stringBuffer.toString();
  }

  public static boolean isUnicode(String str) {
    return str.contains("\\u");
  }

  public static  String unicodeToUtf8(String str) {
    try {
      if (str.contains("\\u")) {
        byte[] bytes = str.getBytes("UTF-8");
        return new String(bytes,"UTF-8");
      } else {
        return str;
      }
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * compare two string equals.
   * @param s1 string 1
   * @param s2  string 2
   * @param emptyAsNull empty as null or not
   * @return equals  boolean
   */
  public static boolean equals(String s1, String s2, boolean emptyAsNull) {
    if (s1 != null && s2 != null) {
      return s1.equals(s2);
    }
    if (emptyAsNull) {
      s1 = s1 == null ? "" : s1;
      s2 = s2 == null ? "" : s2;
      return s1.equals(s2);
    }
    if (s1 != null && s2 == null) {
      return false;
    }

    if (s1 == null && s2 != null) {
      return false;
    }

    return s1.equals(s2);
  }

  /**
   * compare two string array ,compare string one by one.
   * @param array1  string array 1
   * @param array2  string array 2
   * @param emptyAsNull empty string as null or not
   * @return equals
   */
  public static boolean equals(String[] array1, String[] array2, boolean emptyAsNull) {
    if (array1.length != array2.length) {
      return false;
    }
    for (int i = 0; i < array1.length; i++) {
      if (!equals(array1[i], array2[i], emptyAsNull)) {
        return false;
      }
    }
    return true;
  }

  
}
